import echarts from 'echarts';
// 引入柱状图
import 'echarts/lib/chart/bar';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import storageApi from '../../../core/storage/storage';

function constructionUnitTopController($rootScope, $scope, $http, homeStatsService, toDoService) {
  'ngInject';

  /*eslint-disable */
  this.queryTenderProgres = () => {
    this.hasProjectInfo = false;
    homeStatsService.queryTenderProgres({ mapParams: {orgId: $rootScope.orgId || storageApi.get('orgId') } }).then((res) => {
      if (res.success && res.rows.length > 0) {
        this.projectInfos = res.rows;
        this.projectInfo = res.rows[0];
        this.pagination = {
          length: res.rows.length,
          index: 0,
        };
        this.hasProjectInfo = true;
        this.startInterval();
      }
    });
  };

  this.startInterval = () => {
    setInterval(() => {
        this.nextProject();
        $scope.$apply();
    }, 5000)
  };

  this.nextProject = () => {
    if (this.pagination.index < this.pagination.length - 1) {
      this.pagination.index ++;
    } else {
      this.pagination.index = 0;
    }
    this.projectInfo =this.projectInfos[this.pagination.index];
  }

  this.preProject = () => {
    if (this.pagination.index == 0) {
      this.pagination.index = this.pagination.length - 1;
    } else {
      this.pagination.index --;
    }
    this.projectInfo = this.projectInfos[this.pagination.index];
  }

  // 代办
  this.queryTodo = () => {
    this.mapParams = {
      orgId: $rootScope.orgId || storageApi.get('orgId'),
      todoType: 0,
    };
    toDoService
      .query({
        mapParams: this.mapParams,
        page: 1,
        size: 10,
      })
      .then((res) => {
        this.todoTotal = res.total;
      });
  };

  this.initChart = () => {
    const option = {
      color: ['#8fd7ee'],
      tooltip : {
        trigger: 'axis'
      },
      // legend: {
      // data: ['安全指数'],
      // },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis : [
        {
          type : 'category',
          boundaryGap : false,
          data : ['3.1','3.2','3.3','3.4','3.5','3.6','3.7']
        }
      ],
      yAxis : [
        {
          type : 'value'
        }
      ],
      series : [
        {
          name:'安全指数',
          type:'line',
          stack: '总量',
          areaStyle: {
            normal: {},
          },
          data:[20, 50, 80, 90, 130, 180, 200]
        }
      ]
    };
    const myChart = echarts.init(document.getElementById('construction-top-month-safety'));
    myChart.setOption(option);
  }

  this.getWeather = () => {
    $.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',() => {
      $.ajax({
      　　type:"GET",
      　　url:'http://api.map.baidu.com/telematics/v3/weather?location='+remote_ip_info.city+'&output=json&callback=JSON_CALLBACK&ak=ryGGzhrZxutxxgj6q4TPjUGq',
      　　dataType:"jsonp",
      　　jsonp:"callback",
      　　success: (data) => {
    　　　if (data.status == 'success' && data.results && data.results.length > 0) {
                this.weatherInfo = {
                  date: data.date,
                  cityName: data.results[0].currentCity,
                  temperature: data.results[0].weather_data[0].temperature,
                  dayPictureUrl: data.results[0].weather_data[0].dayPictureUrl,
                }
              }
      　　}
      });
    });
  }

  this.initChart();
  this.queryTenderProgres();
  this.getWeather();
  this.queryTodo();
}

export default constructionUnitTopController;
